
// class TreeNode {
//     val: number
//     left: TreeNode | null
//     right: TreeNode | null
//     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
//         this.val = (val === undefined ? 0 : val)
//         this.left = (left === undefined ? null : left)
//         this.right = (right === undefined ? null : right)
//     }
// }

// function preOrder(node: TreeNode | null, set: number[]): void {

//     if (node == null) {
//         return;
//     }
//     set.push(node.val);
//     preOrder(node.left, set);
//     preOrder(node.right, set);
// }


// 层序遍历
function levelOrder(root: TreeNode | null): number[] {
    let result: number[] = new Array<number>();
    let set: TreeNode[] = new Array<TreeNode>();
    if (root == null) {
        return result;
    }
    set.push(root);

    while (set.length > 0) {
        let node = set.shift();
        if (node != null) {
            result.push(node.val);
            if (node.left != null) {
                set.push(node.left);
            }
            if (node.right != null) {
                set.push(node.right);
            }
        }
    }
    return result;
};